var redis = require('redis');
var client = redis.createClient();

// 函数markDealAsSent将用户添加到交易集
function markDealAsSent(dealId, userId) {
    client.sadd(dealId, userId);
}

// sendDealIfNotSent函数检查是否用户ID属于交易集
function sendDealIfNotSent(dealId, userId) { 
  client.sismember(dealId, userId, function(err, reply) { 
    if (reply) {
      console.log("交易集", dealId, "已经存在", userId);
    } else {
      console.log("在", dealId, "中加入", userId); 
      markDealAsSent(dealId, userId); 
    } 
  });
}



// showUsersThatReceivedAllDeals显示存在于所有用户ID 的所有指定的交易集
function showUsersThatReceivedAllDeals(dealIds) {
    client.sinter(dealIds, function (err, reply) {
        console.log(reply + " received all of the deals: " + dealIds);
    })
}


// showUsersThatReceivedAtLeastOneOfTheDeals显示指定的任何交易集中存在的所有用户标识
function showUsersThatReceivedAtLeastOneOfTheDeals(dealIds) {
    client.sunion(dealIds, function (err, reply) {
        console.log(reply + " received at least one of the deals: " + dealIds);
    })
}

markDealAsSent('deal:1', 'user:1');
markDealAsSent('deal:1', 'user:2');
markDealAsSent('deal:2', 'user:1');
markDealAsSent('deal:2', 'user:3');

sendDealIfNotSent('deal:1', 'user:1');
sendDealIfNotSent('deal:1', 'user:2');
sendDealIfNotSent('deal:1', 'user:3');


showUsersThatReceivedAllDeals(["deal:1", "deal:2"]);
showUsersThatReceivedAtLeastOneOfTheDeals(["deal:1", "deal:2"]);

client.quit();

